package com.ts.animal.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.ts.animal.constant.AnimalConstant;
import com.ts.animal.dao.ExRfidLabelUseInfoDao;
import com.ts.animal.dao.RfidLabelUseInfoDao;
import com.ts.animal.domain.ExcelRfidLabelUseInfoDO;
import com.ts.animal.domain.RfidLabelSettlementDO;
import com.ts.animal.domain.RfidLabelUseInfoDO;
import com.ts.common.service.IWaterCodeService;
import com.ts.common.utils.UtilString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ts.animal.dao.RfidLabelImportDao;
import com.ts.animal.domain.RfidLabelImportDO;
import com.ts.animal.service.RfidLabelImportService;
import com.ts.common.utils.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@Service
public class RfidLabelImportServiceImpl implements RfidLabelImportService {
    @Autowired
    private RfidLabelImportDao rfidLabelImportDao;
    @Autowired
    private ExRfidLabelUseInfoDao exRfidLabelUseInfoDao;
    @Resource
    private IWaterCodeService iWaterCodeService;
    @Autowired
    private RfidLabelUseInfoDao rfidLabelUseInfoDao;

    @Override
    public RfidLabelImportDO get(Integer id) {
        return rfidLabelImportDao.get(id);
    }

    @Override
    public List<Map<String, Object>> list(Map<String, Object> map) {
        map.put("useStatusYes", AnimalConstant.use_status_yes);
        map.put("useStatusNot", AnimalConstant.use_status_not);
        map.put("useStatusTest", AnimalConstant.use_status_test);
        return exRfidLabelUseInfoDao.importLabelList(map);
    }

    @Override
    public Map<String, Object> importLabelSum(Map<String, Object> map) {
        map.put("useStatusYes", AnimalConstant.use_status_yes);
        map.put("useStatusNot", AnimalConstant.use_status_not);
        map.put("useStatusTest", AnimalConstant.use_status_test);
        return exRfidLabelUseInfoDao.importLabelSum(map);
    }

    @Override
    public int count(Map<String, Object> map) {
        return rfidLabelImportDao.count(map);
    }

    @Override
    public int save(Result result, RfidLabelImportDO rfidLabelImport) {
        int ret = rfidLabelImportDao.save(rfidLabelImport);
        if (ret == 0) {
            result.set(Result.FAIL, "保存失败", null);
        }
        return ret;


    }

    @Override
    public int update(Result result, RfidLabelImportDO rfidLabelImport) {
        int ret = rfidLabelImportDao.update(rfidLabelImport);
        if (ret == 0) {
            result.set(Result.FAIL, "更新失败", null);
        }
        return ret;
    }

    @Override
    public int remove(Result result, Integer id) {
        int ret = rfidLabelImportDao.remove(id);
        if (ret == 0) {
            result.set(Result.FAIL, "删除失败", null);
        }
        return ret;
    }

    @Override
    public int batchRemove(Result result, Integer[] ids) {
        for (Integer id : ids) {
            RfidLabelImportDO rfidLabelImport = rfidLabelImportDao.get(id);
            rfidLabelImportDao.remove(id);
            exRfidLabelUseInfoDao.deleteLabelByImportId(rfidLabelImport.getId());
        }
        return 1;
    }

    @Override
    @Transactional
    public Result batchImportLabel(MultipartFile file) throws Exception {
        Result result = Result.ok();
        String importBatchNum = iWaterCodeService.generate("L", "importLabel", 8);
        Date now = new Date();

        //插入录入信息
        RfidLabelImportDO rfidLabelImportDO = new RfidLabelImportDO();
        rfidLabelImportDO.setImportBatchNum(importBatchNum);
        rfidLabelImportDO.setImportDatetime(now);
        rfidLabelImportDao.save(rfidLabelImportDO);

        EasyExcel.read(file.getInputStream(), ExcelRfidLabelUseInfoDO.class, new ReadListener<ExcelRfidLabelUseInfoDO>() {
            /**
             * 单次缓存的数据量
             */
            public static final int BATCH_COUNT = 500;

            int updateRow = 0;
            /**
             *临时存储
             */
            private List<ExcelRfidLabelUseInfoDO> labelUseInfoList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

            @Override
            public void invoke(ExcelRfidLabelUseInfoDO rfidLabelUseInfoDO, AnalysisContext context) {
                rfidLabelUseInfoDO.setImportId(rfidLabelImportDO.getId());
                rfidLabelUseInfoDO.setUseStatus(AnimalConstant.use_status_not);
                if(UtilString.isNotEmpty(rfidLabelUseInfoDO.getNum())){
                    labelUseInfoList.add(rfidLabelUseInfoDO);
                }
                if (labelUseInfoList.size() >= BATCH_COUNT) {
                    saveData();
                    // 存储完成清理 list
                    labelUseInfoList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
                }
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
                saveData();
                result.setMsg("成功录入" + updateRow + "个耳标");
            }

            /**
             * 加上存储数据库
             */
            private void saveData() {
                updateRow = updateRow + exRfidLabelUseInfoDao.batchImportLabel(labelUseInfoList);
            }

        }).sheet().doRead();
        return result;
    }

}
